@echo off
rem WARNING: Use this script at your own risk!

:parameters
rem Descriptive script name
set NAME=xbprint

set MSBUILD="C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe"
set MSBUILD4="C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe"
set ILMERGE="utils\ilmerge.exe"

set CONFIG=Release
set PLATFORM=AnyCPU

rem /v:<verbosity> - q[uiet], m[inimal], n[ormal], d[etailed], diag[nostic]
set VERBOSE=m

set SOLUTION=%NAME%.sln
rem CWD - Current working directory, should be the solution directory when publishing, but can be other when deploying
set CWD=%CD%
rem CPU_CORE_COUNT - Number of CPU cores for faster builds
set CPU_CORE_COUNT=2

:validation
if not "%1"=="" goto %1
if %CWD%=="" goto error-cwd-missing
if not exist %MSBUILD% goto error-msbuild-missing
if not exist %SOLUTION% goto error-solution-missing
goto default

:default
:build
call %0 wpf
call %0 blend3
call %0 blend4
call %0 kaxaml
goto end

:wpf
if not exist %SOLUTION% goto error-solution-missing
if "%1"=="%NAME%" set CONFIG=%2
if "%1"=="%NAME%" set PLATFORM=%3
if "%CONFIG%"=="" set CONFIG=Release
if "%PLATFORM%"=="" set PLATFORM=AnyCPU
echo --------------------------------------------------------------------------------
echo Building '%NAME%' (%CONFIG%^|%PLATFORM%)...

set CSPROJ_PATH=xbprint\Oxage.Printing.WPF\Oxage.Printing.WPF.csproj
set CSPROJ_REL=..\..\
set OUT_DIR=bin\wpf

if exist "%OUT_DIR%" rmdir /S /Q "%OUT_DIR%"
%MSBUILD% %CSPROJ_PATH% /t:Rebuild /p:OutDir=%CSPROJ_REL%%OUT_DIR%\ /p:WarningLevel=1;Configuration=%CONFIG%;Platform=%PLATFORM% /v:%VERBOSE% /m:%CPU_CORE_COUNT% /nologo

rem Preserve only needed files
del /S /F /Q %OUT_DIR%\*.pdb

cd %CWD%

goto end

:blend3
if not exist %SOLUTION% goto error-solution-missing
if "%1"=="%NAME%" set CONFIG=%2
if "%1"=="%NAME%" set PLATFORM=%3
if "%CONFIG%"=="" set CONFIG=Release
if "%PLATFORM%"=="" set PLATFORM=AnyCPU
echo --------------------------------------------------------------------------------
echo Building '%NAME%' (%CONFIG%^|%PLATFORM%)...

set CSPROJ_PATH_BLEND3=xbprint\Oxage.Printing.Blend.3.AddIn\Oxage.Printing.Blend.3.AddIn.csproj
set CSPROJ_REL=..\..\
set OUT_DIR_BLEND3=bin\blend3

if exist "%OUT_DIR_BLEND3%" rmdir /S /Q "%OUT_DIR_BLEND3%"
%MSBUILD% %CSPROJ_PATH_BLEND3% /t:Rebuild /p:OutDir=%CSPROJ_REL%%OUT_DIR_BLEND3%\tmp\ /p:WarningLevel=1;Configuration=%CONFIG%;Platform=%PLATFORM% /v:%VERBOSE% /m:%CPU_CORE_COUNT% /nologo

rem Preserve only needed files
del /S /F /Q %OUT_DIR_BLEND3%\*.pdb
copy %OUT_DIR_BLEND3%\tmp\Oxage.Printing.Blend.AddIn.dll %OUT_DIR_BLEND3%\Oxage.Printing.Blend.AddIn.dll
copy %OUT_DIR_BLEND3%\tmp\Oxage.Printing.Control.dll %OUT_DIR_BLEND3%\Oxage.Printing.Control.dll
copy xbprint\Oxage.Printing.Blend.3.AddIn\Print.Blend.Addin %OUT_DIR_BLEND3%\Print.Blend.Addin
rmdir /S /Q %OUT_DIR_BLEND3%\tmp

cd %CWD%

goto end

:blend4
if not exist %SOLUTION% goto error-solution-missing
if "%1"=="%NAME%" set CONFIG=%2
if "%1"=="%NAME%" set PLATFORM=%3
if "%CONFIG%"=="" set CONFIG=Release
if "%PLATFORM%"=="" set PLATFORM=AnyCPU
echo --------------------------------------------------------------------------------
echo Building '%NAME%' (%CONFIG%^|%PLATFORM%)...

set CSPROJ_PATH_BLEND4=xbprint\Oxage.Printing.Blend.4.AddIn\Oxage.Printing.Blend.4.AddIn.4.0.csproj
set CSPROJ_REL=..\..\
set OUT_DIR_BLEND4=bin\blend4

if exist "%OUT_DIR_BLEND4%" rmdir /S /Q "%OUT_DIR_BLEND4%"
%MSBUILD4% %CSPROJ_PATH_BLEND4% /t:Rebuild /p:OutDir=%CSPROJ_REL%%OUT_DIR_BLEND4%\tmp\ /p:DefineConstants="BLEND4";WarningLevel=1;Configuration=%CONFIG%;Platform=%PLATFORM% /v:%VERBOSE% /m:%CPU_CORE_COUNT% /nologo

rem Preserve only needed files
del /S /F /Q %OUT_DIR_BLEND4%\*.pdb
copy %OUT_DIR_BLEND4%\tmp\Oxage.Printing.Blend.AddIn.dll %OUT_DIR_BLEND4%\Oxage.Printing.Blend.AddIn.dll
copy %OUT_DIR_BLEND4%\tmp\Oxage.Printing.Control.dll %OUT_DIR_BLEND4%\Oxage.Printing.Control.dll
copy xbprint\Oxage.Printing.Blend.4.AddIn\Print.Blend.Addin %OUT_DIR_BLEND4%\Print.Blend.Addin
rmdir /S /Q %OUT_DIR_BLEND4%\tmp

cd %CWD%

goto end

:kaxaml
if not exist %SOLUTION% goto error-solution-missing
if "%1"=="%NAME%" set CONFIG=%2
if "%1"=="%NAME%" set PLATFORM=%3
if "%CONFIG%"=="" set CONFIG=Release
if "%PLATFORM%"=="" set PLATFORM=AnyCPU
echo --------------------------------------------------------------------------------
echo Building '%NAME%' (%CONFIG%^|%PLATFORM%)...

set CSPROJ_PATH_KAXAML=xbprint\Kaxaml.Plugins.PrintPlugin\Kaxaml.Plugins.PrintPlugin.csproj
set CSPROJ_REL=..\..\
set OUT_DIR_KAXAML=bin\kaxaml

if exist "%OUT_DIR_KAXAML%" rmdir /S /Q "%OUT_DIR_KAXAML%"
if not exist "%OUT_DIR_KAXAML%\Plugins" mkdir "%OUT_DIR_KAXAML%\Plugins"
%MSBUILD% %CSPROJ_PATH_KAXAML% /t:Rebuild /p:OutDir=%CSPROJ_REL%%OUT_DIR_KAXAML%\tmp\ /p:WarningLevel=1;Configuration=%CONFIG%;Platform=%PLATFORM% /v:%VERBOSE% /m:%CPU_CORE_COUNT% /nologo

rem Preserve only needed files
del /S /F /Q %OUT_DIR_KAXAML%\*.pdb
copy %OUT_DIR_KAXAML%\tmp\PrintPlugin.dll %OUT_DIR_KAXAML%\Plugins\PrintPlugin.dll
copy %OUT_DIR_KAXAML%\tmp\Oxage.Printing.Control.dll %OUT_DIR_KAXAML%\Oxage.Printing.Control.dll
rmdir /S /Q %OUT_DIR_KAXAML%\tmp

cd %CWD%

goto end

:clean
if not exist %SOLUTION% goto error-solution-missing
echo Cleaning...
%MSBUILD% %SOLUTION% /t:Clean /p:WarningLevel=1;Configuration=Debug;Platform="Any CPU" /v:m /m:%CPU_CORE_COUNT% /nologo
%MSBUILD% %SOLUTION% /t:Clean /p:WarningLevel=1;Configuration=Release;Platform="Any CPU" /v:m /m:%CPU_CORE_COUNT% /nologo
echo Completed.
goto end

:unbind
rem Remove all files for SourceSafe reference
echo Removing all SourceSafe files...
del /S /F /Q *.vssscc
del /S /F /Q *.vspscc
del /S /F /Q *.scc
del /S /F /Q *.suo
del /S /F /Q *.user
del /S /F /Q /A:H *.scc
del /S /F /Q /A:H *.suo
for /F %%i in ('dir /S /B /A:H .svn') do rmdir /S /Q %%i
goto end

:help
:-help
:-?
:/?
:/help
:usage
echo Usage:
echo   make [ help ^| clean ^| build ^| unbind ]
echo.
echo Options:
echo   help      this message
echo   clean     remove all bin\* and obj\* files
echo   build     build all projects in the solution
echo   unbind    remove all SourceSafe and SVN bindings
echo.
echo Notes:
echo   For 'clean' and 'build' options the script must be
echo   in the solution directory (%SOLUTION%)
goto end

:error-solution-missing
echo Error: Cannot find %SOLUTION%
echo Search location: %CD%
echo Tip: copy this script to the solution directory
goto end

:error-msbuild-missing
echo Error: Cannot find MSBuild.exe
echo Search location: %MSBUILD%
goto end

:error-cwd-missing
echo Error: cannot get current working directory
goto end

:end-error
echo Error!
goto end

:end